package sheep.utils.fileio;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfCell;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import sheep.controller.Workspace;
import sheep.model.Worker;

public class PDFExporter {
  private Workspace workSpace;
  Document document;
  PdfWriter writer;
  
  
  public PDFExporter(Workspace workSpace) {
    this.workSpace = workSpace;
    document = new Document(); // Crée un document en page A4
  }
  
  public void export() {
    try {
      // Création du fichier
      writer = PdfWriter.getInstance(document, new FileOutputStream(workSpace.getSavePath() + "test.pdf"));
      document.open();
      
      // On inscrit le titre de la page
      Chunk c = new Chunk("SHEEP Report");
      c.setFont(new Font(Font.HELVETICA, 26));
      Paragraph title = new Paragraph();
      title.add(c);
      title.setAlignment(Element.ALIGN_CENTER);
      title.add(Chunk.NEWLINE);
      title.add(Chunk.NEWLINE);
      document.add(title);
      
      // On crée la liste des users
      Chunk uTitle = new Chunk("Collaborateurs");
      uTitle.setFont(new Font(Font.HELVETICA, 16));
      Chunk[] labels = { new Chunk("Numéro: "), new Chunk("Nom: "), new Chunk("Prenom: "), new Chunk("Date de naissance: "),
      new Chunk("Adresse: "), new Chunk("Job: ")};
      for(Chunk ch : labels) ch.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, new Color(0x00, 0x00, 0x00));
      
      document.add(uTitle);
      document.add(Chunk.NEWLINE);
      
      // Creation des users
      for(Worker w : workSpace.getWorkersList()) {
        PdfPTable uTable = new PdfPTable(2);
        uTable.getDefaultCell().setBorderColor(new Color(255, 255, 255)); // Bordures transparentes
        Paragraph userP = new Paragraph(); // Un paragraphe par employé
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE); // Pour l'affichage de la date de naissance
        Chunk[] user = { new Chunk(String.valueOf(w.getId())), new Chunk(w.getLastName()),
            new Chunk(w.getFirstName()), 
            new Chunk(df.format(w.getBirthDate().getTime())),
            new Chunk(w.getAdresse() + Chunk.NEWLINE + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + w.getPostCode() + " " + w.getTown()),
            new Chunk(w.getJob())};
        Image uImg = Image.getInstance(w.getPicturesURL());
        uImg.scaleAbsolute(100.0f, 100.0f);
        
        // Ajout des users au pdf
        for(int i=0; i<labels.length; i++) {
          userP.add(labels[i]);
          userP.add(user[i]);
          userP.add(Chunk.NEWLINE);
        }
        
        uTable.addCell(new PdfPCell(uImg));
        uTable.addCell(new PdfPCell(userP));
        document.add(uTable);
        document.add(Chunk.NEWLINE);
      }
      
      
      
      // On ferme le fichier
      document.close();
    
    } catch (BadElementException ex) {
      Logger.getLogger(PDFExporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MalformedURLException ex) {
      Logger.getLogger(PDFExporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(PDFExporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
      Logger.getLogger(PDFExporter.class.getName()).log(Level.SEVERE, null, ex);
    }

  }
  
  
}